import { useI18n } from "vue-i18n";

import type * as type from "./type";
export function parseMsg(message?: string | null): string {
	// 空值处理
	if (!message?.trim()) {
		return "车端返回空";
	}

	// 简单消息直接返回
	if (!message.includes(": ")) {
		return message;
	}

	// 解析多语言映射
	const langMap = message.split("\n").reduce<type.LangMap>((acc, line) => {
		const [key, value] = line.split(": ");
		if (key && value) {
			acc[key.toLowerCase()] = value.replace(/"/g, "").trim();
		}
		return acc;
	}, {});
	const { locale } = useI18n();
	// 根据语言返回对应文本
	const currentLocale = locale.value;
	switch (currentLocale) {
		case "zh":
			return langMap.zh || message;
		default:
			return langMap.en || message;
	}
}
export default parseMsg;
